home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1991 / 06 / dflat3 / msgbox.c < prev    next >
Text File  |  1991-05-19  |  4KB  |  168 lines

  1. /* ------------------ msgbox.c ------------------ */
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include "dflat.h"
  7.  
  8. extern DBOX MsgBox;
  9.  
  10. static int ReturnValue;
  11.  
  12. int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  13. {
  14.     switch (msg)    {
  15.         case CREATE_WINDOW:
  16.             GetClass(wnd) = MESSAGEBOX;
  17.             ClearAttribute(wnd, CONTROLBOX);
  18.             break;
  19. #ifdef INCLUDE_DIALOG_BOXES
  20.         case KEYBOARD:
  21.             if (p1 == '\r' || p1 == ESC)
  22.                 ReturnValue = (int)p1;
  23.             break;
  24. #endif
  25.         default:
  26.             break;
  27.     }
  28.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  29. }
  30.  
  31. int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  32. {
  33.     switch (msg)    {
  34.         case CREATE_WINDOW:
  35.             GetClass(wnd) = MESSAGEBOX;
  36.             ClearAttribute(wnd, CONTROLBOX);
  37.             break;
  38.         case KEYBOARD:    {
  39.             int c = tolower((int)p1);
  40.             if (c == 'y')
  41.                 SendMessage(wnd, COMMAND, ID_OK, 0);
  42.             else if (c == 'n')
  43.                 SendMessage(wnd, COMMAND, ID_CANCEL, 0);
  44.             break;
  45.         }
  46. #ifndef INCLUDE_DIALOG_BOXES
  47.         case COMMAND:
  48.             if (p1 == ID_OK || p1 == ID_CANCEL)    {
  49.                 ReturnValue = (int)p1;
  50.                 return TRUE;
  51.             }
  52.             break;
  53. #endif
  54.         default:
  55.             break;
  56.     }
  57.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  58. }
  59.  
  60. int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  61. {
  62.     switch (msg)    {
  63.         case CREATE_WINDOW:
  64.             GetClass(wnd) = ERRORBOX;
  65.             ClearAttribute(wnd, MOVEABLE | CONTROLBOX);
  66.             break;
  67. #ifdef INCLUDE_DIALOG_BOXES
  68.         case KEYBOARD:
  69.             if (p1 == '\r' || p1 == ESC)
  70.                 ReturnValue = (int)p1;
  71.             break;
  72. #endif
  73.         default:
  74.             break;
  75.     }
  76.     return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
  77. }
  78.  
  79. static int GenericMessage(char *ttl, char *msg, int buttonct,
  80.     int (*wndproc)(struct window *, enum messages, PARAM, PARAM),
  81.     char *b1, char *b2)
  82. {
  83. #ifdef INCLUDE_DIALOG_BOXES
  84.     int rtn;
  85.     MsgBox.dwnd.title = ttl;
  86.     MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
  87.     MsgBox.ctl[0].dwnd.w = max(MsgWidth(msg),
  88.             buttonct*8 + buttonct + 2);
  89.     MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
  90.     MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
  91.     if (buttonct == 1)
  92.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
  93.     else    {
  94.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 18) / 2;
  95.         MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 9;
  96.         MsgBox.ctl[2].class = BUTTON;
  97.     }
  98.     MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
  99.     MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
  100.     MsgBox.ctl[0].itext = msg;
  101.     MsgBox.ctl[1].itext = b1;
  102.     MsgBox.ctl[2].itext = b2;
  103.     rtn = DialogBox(NULLWND, &MsgBox, wndproc);
  104.     MsgBox.ctl[2].class = 0;
  105.     return rtn;
  106. #else
  107.     WINDOW wnd;
  108.  
  109.     wnd = CreateWindow(TEXTBOX,ttl,
  110.             -1,-1,MsgHeight(msg)+3,MsgWidth(msg)+2,
  111.             NULL,NULL,
  112.             wndproc,
  113.             HASBORDER | TITLEBAR | MOVEABLE);
  114.     SendMessage(wnd, SETTEXT, LPARAM(msg), 0);
  115.     SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  116.     SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  117.     ReturnValue = 0;
  118.     while (ReturnValue == 0)
  119.         ;
  120.     SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  121.     SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  122.     return ReturnValue == ID_OK || ReturnValue == '\r';
  123. #endif
  124. }
  125.  
  126. int TestErrorMessage(char *msg)
  127. {
  128.     return GenericMessage("Error", msg, 2, ErrorBoxProc, Ok, Cancel);
  129. }
  130.  
  131. void ErrorMessage(char *msg)
  132. {
  133.     GenericMessage("Error", msg, 1, ErrorBoxProc, Ok, NULL);
  134. }
  135.  
  136. void MessageBox(char *ttl, char *msg)
  137. {
  138.     GenericMessage(ttl, msg, 1, MessageBoxProc, Ok, NULL);
  139. }
  140.  
  141. int YesNoBox(char *msg)
  142. {
  143.     return GenericMessage(NULL, msg, 2, YesNoBoxProc, Yes, No);
  144. }
  145.  
  146. int MsgHeight(char *msg)
  147. {
  148.     int h = 1;
  149.     while ((msg = strchr(msg, '\n')) != NULL)    {
  150.         h++;
  151.         msg++;
  152.     }
  153.     return h;
  154. }
  155.  
  156. int MsgWidth(char *msg)
  157. {
  158.     int w = 0;
  159.     char *cp = msg;
  160.     while ((cp = strchr(msg, '\n')) != NULL)    {
  161.         w = max(w, (int) (cp-msg));
  162.         msg = cp+1;
  163.     }
  164.     return max(strlen(msg),w);
  165. }
  166.  
  167.  
  168.